package com.spring.demo.untils.document;

import lombok.Data;

import java.util.function.Function;

/**
 * @author xuweizhi
 * @since 2023/04/26 14:06
 */
@Data
public class OrderMapping<T,R> implements Comparable<OrderMapping> {

    /**
     * file split index
     */
    private Integer valueIndex;

    /**
     * file split index
     */
    private Integer keyIndex;

    /**
     * 模板 index
     */
    private Integer templateIndex;

    /**
     * 关联 key
     */
    private String relationKeyIndex;

    private String filedName;

    private Function<T, R> function;


    public OrderMapping(Integer keyIndex, Integer templateIndex) {
        this.keyIndex = keyIndex;
        this.templateIndex = templateIndex;
    }


    public OrderMapping(Integer keyIndex, Integer templateIndex, String relationKeyIndex) {
        this.keyIndex = keyIndex;
        this.templateIndex = templateIndex;
        this.relationKeyIndex = relationKeyIndex;
    }

    @Override
    public int compareTo(OrderMapping orderMapping) {
        return orderMapping == null ? 0 : this.templateIndex - orderMapping.getTemplateIndex();
    }


    public <T, R> R handle(Function<T, R> function, T data) {
        if (relationKeyIndex != null) {
            return function.apply(data);
        }
        return null;
    }
}
